package com.goodedeeds.webapp.controller;

import com.goodedeeds.domain.resource.Project;
import com.youtills.core.admin.ILookup;
import com.youtills.core.admin.LookupType;
import com.youtills.core.admin.db.dao.AdminDao;
import com.youtills.core.lang.NumberUtil;
import com.youtills.resource.ResourceManager;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: rchiluve
 * Date: 2/23/14
 * Time: 8:15 PM
 * To change this template use File | Settings | File Templates.
 */
@UrlBinding(ProjectAction.URL)
public class ProjectAction extends BaseDashboardAction {

    public static final String URL = "/project.action";

    private static final String VIEW = JSP_ROOT + "/project-view.jsp";
    private static final String EDIT_VIEW = JSP_ROOT + "/project-edit.jsp";
    private static final String LIST_VIEW = JSP_ROOT + "/project-list.jsp";

    private Long projectId;
    private Project project;
    private Long parentResourceId;
    private Long parentResourceType;

    private String dashboardContent;

    @Override
    public String getDashboardContent() {
        return dashboardContent;
    }

    @Override
    public DashboardTab getCurrentTab() {
        return DashboardTab.NONE;
    }

    public Long getProjectId() {
        if(projectId == null) {
            projectId = NumberUtil.parseLong(getRequestParameter("projectId"));
        }
        return projectId;
    }

    public Project getProject() {

        if(project == null) {
            if ( getProjectId() == null){
//                project = DomainFactory.getProjectDao().newProject(null, getParentResourceType(), getParentResourceId());
//                project = ResourceManager.instance.newChildResource(getActionContext(), Project.class, getParentResourceType(), getParentResourceId());
                project = ResourceManager.instance.newResource(getActionContext(), Project.class);
                project.addResourceId(getParentResourceType(), getParentResourceId());

            } else {
//                project = DomainFactory.getProjectDao().getProject(null, getProjectId());
                project = ResourceManager.instance.getResource(getActionContext(), Project.class, getProjectId());
            }
        }
        return project;
    }

    public Long getParentResourceId() {
        if(parentResourceId == null) {
            parentResourceId = NumberUtil.parseLong(getRequestParameter("parentResourceId"));
        }
        return parentResourceId;
    }

    public Long getParentResourceType() {
        if(parentResourceType == null) {
            parentResourceType = NumberUtil.parseLong(getRequestParameter("parentResourceType"));
        }

        return parentResourceType;
    }

    public Resolution save(){
//        DomainFactory.getProjectDao().saveProject(null, project);
        ResourceManager.instance.saveResource(getActionContext(), getProject());
        projectId = project.getProjectId();
        RedirectResolution res = new RedirectResolution(ProjectAction.class, "view");
        res.addParameter("projectId", projectId);
        return res;
    }

    public Resolution view(){
        dashboardContent = VIEW;
        return showPage();
    }

    public Resolution edit() {
        dashboardContent = EDIT_VIEW;
        return showPage();
    }

    private List<ILookup> projectStatuses;
    public List<ILookup> getProjectStatuses(){
        if(projectStatuses == null){
            projectStatuses = AdminDao.getInstance().getLookups(LookupType.getByType(6L));
        }

        return projectStatuses;
    }
}
